<?php
namespace app\api\controller;
use app\common\controller\Api;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\Writer\PngWriter;
use think\Request;
use think\Db;
/**
 * 购物车
 */
class Getqrcode extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];



    public function getqrcode(){


        $id=$this->request->param('id');


        $appId        = 'wxc727205a0a40319a';
        $appSecret    = 'fef7fae94090719e8f658952454711fc';

        $access_token = $this->getaccess_token($appId, $appSecret);

        $access_token = $access_token['access_token'];

        $request_url = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$access_token;

        $request_data = array(
            'scene' => $id,
            'page' => 'pages/project/info',
            'width' => '690'
        );

        $img =$this->https_request($request_url, $request_data, 'json');


        $imgfile="/uploads/qrcode/".md5(1).".jpg";

        $filename =  ".".$imgfile;//要生成的图片名字


        $file = fopen($filename, "w");//打开文件准备写入

        fwrite($file, $img);//写入

        fclose($file);//关闭

        $this->success('太阳码',cdnurl($imgfile,true));

    }


    public function getaccess_token($appid, $appsecret)  {

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, 0);
        // 关闭https验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        //The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $output = curl_exec($ch);
        curl_close($ch);

        return json_decode($output, true);


        // close curl resource to free up system resources
    }

    public function https_request($url = '', $param = [], $contentType = ''){
        $ch = curl_init();

        // 请求地址
        curl_setopt($ch, CURLOPT_URL, $url);

        // 请求参数类型
        $param = $contentType == 'json' ? json_encode($param) : $param;

        // 关闭https验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        // post提交
        if ($param) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        }

        // 返回的数据是否自动显示
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // 执行并接收响应结果
        $output = curl_exec($ch);

        // 关闭curl
        curl_close($ch);

        return $output !== false ? $output : false;
    }



    /**
     * @return 生成 标准二维码
     */
    public function generateQrCode()
    {
        // 获取要生成二维码的内容
        $text =$this->request->param('code');

        if (empty($text)) {
            $this->error('请提供要生成二维码的内容');
        }

        // 创建二维码实例
        $qrCode = QrCode::create($text);

        // 创建 PNG 写入器
        $writer = new PngWriter();

        // 生成二维码图片
        $result = $writer->write($qrCode);

        // 获取图片的二进制数据
        $imageData = $result->getString();


        $imgfile="/uploads/qrcode/".md5(1).".jpg";

        $filename =  ".".$imgfile;//要生成的图片名字


        $file = fopen($filename, "w");//打开文件准备写入

        fwrite($file, $imageData);//写入

        fclose($file);//关闭

        $this->success('太阳码',cdnurl($imgfile,true));


    }



}